<template>
	<form class="bind-mobile-modal px-[25px] md:px-[35px] pt-4 pb-10" @submit.prevent="onSubmit">
		<input type="submit" class="hidden" />
		<BaseInput
			labelT="account.email"
			v-model="formValue.email"
			:name="FormFieldName.email"
			:readonly="!!profileStore.userInfo?.email" />
		<BaseInput
			labelT="account.emailCode"
			v-model="formValue.verifyCode"
			:name="FormFieldName.verifyEmailCode">
			<template #suffix>
				<BaseCodeEmail :email="formValue.email" />
			</template>
		</BaseInput>
		<BaseButton
			class="mt-10"
			type="primary"
			textT="action.confirm"
			:loading="loading"
			@click="onSubmit" />
	</form>
</template>

<script setup lang="ts">
import { reactive } from 'vue'
import { updateEmail } from '@/api/account.ts'
import { BaseButton, BaseCodeEmail, BaseInput } from '@/components/base'
import { FormFieldName } from '@/enums'
import { useRequest } from '@/hooks'
import { useProfileStore } from '@/store'
import { t } from '@/utils'

const profileStore = useProfileStore()
const formValue = reactive({
	email: profileStore.userInfo?.email || '',
	verifyCode: ''
})

const { run, loading } = useRequest(updateEmail, {
	showError: true,
	showSuccess: t('tip.verifySuccess'),
	onSuccess: () => {
		profileStore.getUserInfo()
		profileStore.show.email = false
	}
})
const onSubmit = () => {
	run({
		email: formValue.email,
		code: formValue.verifyCode
	})
}
</script>

<style scoped></style>
